<template>
  <a-drawer v-model:open="open" :mask-closable="false" class="custom-class" root-class-name="root-class-name"
    width="30%" :title="props.type == 'add' ? '新增分类' : '编辑分类'" placement="right" @close="closebtn">
    <a-form ref="formRef" :model="formState.info" :rules="rules" layout="vertical">
      <a-form-item ref="icon" label="图标" name="icon">
        <uploadImg v-model:value="formState.info.icon" />
      </a-form-item>
      <a-form-item ref="name" label="名称" name="name">
        <a-input v-model:value="formState.info.name" />
      </a-form-item>
      <a-form-item ref="parentId" label="分类" name="parentId"
        v-if="props.type == 'add' || (props.type == 'update' && props.editdata?.parentid.length)">
        <a-cascader v-model:value="formState.info.parentId" change-on-select :options="cataslist"
          placeholder="请选择所属分类" />
      </a-form-item>
      <a-form-item ref="description" label="备注" name="description">
        <a-textarea v-model:value="formState.info.description" :rows="4" placeholder="请输入备注" />
      </a-form-item>
    </a-form>
    <template #footer>
      <div style="display: flex; justify-content: flex-end">
        <a-button style="margin-right: 8px" @click="closebtn">取消</a-button>
        <a-button type="primary" :loading="loading" @click="infobtn">确定</a-button>
      </div>
    </template>
  </a-drawer>
</template>

<script setup lang="ts">
import { computed, reactive, ref, watch, inject } from "vue";

import { message } from "ant-design-vue";

import * as api from "#/api/core/class";
import uploadImg from "#/components/uploadImg/index.vue";

const type = inject("classtype");

const props = defineProps({
  value: {
    type: Boolean,
    default: false,
  },
  type: {
    type: String,
    default: "add",
  },
  editdata: {
    type: Object,
    default: () => { },
  },
  catalist: {
    type: Array,
    default: () => []
  }
});

const emits = defineEmits(["update:value", "submit", "close"]);

const formRef = ref();
const rules = {
  name: [
    {
      required: true,
      message: "请输入名称",
      trigger: "change",
    },
  ],
  description: [
    {
      required: false,
      message: "请输入备注",
      trigger: "change",
    },
  ],
};



const formState = reactive({
  info: {
    icon: "",
    name: "",
    description: "",
    parentId: '',
  },
});

const loading = ref(false);

watch(
  () => props.value,
  (newV) => {
    console.log("props", props.editdata);

    if (newV && props.type == "update") {
      formState.info.name = props.editdata?.label
      formState.info.description = props.editdata?.extend?.desc
      formState.info.icon = props.editdata?.extend?.icon
      formState.info.parentId = props.editdata.parentid
    } else {
      formState.info.name = "";
      formState.info.description = "";
      formState.info.icon = "";
      formState.info.parentId = ''
    }
  },
  {
    immediate: true,
  }
);

const open = computed({
  get() {
    return props.value;
  },
  set(val) {
    emits("update:value", val);
  },
});



const cataslist = computed({
  get() {
    return props.catalist
  },
})


const infobtn = () => {
  console.log("info", formState.info);

  formRef.value
    .validate()
    .then(async () => {
      loading.value = true;
      try {

        let parantid = formState.info.parentId[formState.info.parentId.length - 1]

        if (props.type == "add") {
          await api.$createlist({
            ...formState.info,
            parentId: parantid,
            type: type,
          });
          message.success("添加成功");
          formState.info.name = "";
          formState.info.description = "";
          formState.info.icon = "";
          open.value = false;
          loading.value = false;
          emits("submit");
        } else {
          await api.$updateclass(
            {
              ...formState.info,
              parentId: parantid,
              type: type,
            },
            props.editdata?.value
          );
          message.success("编辑成功");
          loading.value = false;

          open.value = false;
          emits("submit");
        }
        // debugger
      } catch (error) {
        console.log("error", error);
        loading.value = false
      }
    })
    .catch((error: any) => {
      console.log("error", error);
    });
};

const closebtn = () => {
  open.value = false;
  formState.info.name = "";
  formState.info.description = "";
  formState.info.icon = "";
  emits("close");
};
</script>
